package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseDepartment;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Department extends BaseDepartment<Department> {

  public static final Department dao = new Department().dao();

  public static ModelResult add(String name, int type) {
    ModelResult result = new ModelResult(Result.kSuccess);
    Department department = search(name, type);
    if (null == department) {
      department = new Department();
      department.setType(type);
      department.setName(name);
      boolean r = department.save();
      if (!r) {
        result.setResult(Result.kModelSaveError);
      }
    }
    result.setObject(department);
    return result;
  }

  public static Department search(String name, int type) {
    Department department = null;
    if (StrKit.notBlank(name) && type > 0) {
      department = dao
          .findFirst("SELECT * FROM department WHERE name = ? and type=?", name,
              type);
    }
    return department;
  }

  public static boolean del(int id) {
    return dao.deleteById(id);
  }

  public static ModelResult update(int id, String name, int type, int charge) {
    ModelResult result = new ModelResult(Result.kNull);
    Department department = dao.findById(id);
    if (null != department) {
      if (type > 0) {
        department.setType(type);
      }
      if (StrKit.notBlank(name)) {
        department.setName(name);
      }
      if (charge > 0) {
        department.setCharge(charge);
      }
      boolean r = department.update();
      if (r) {
        result.setResult(Result.kSuccess);
        result.setObject(department);
      }
    }
    return result;
  }

  public static ModelResult query(int id) {
    ModelResult result = new ModelResult(Result.kNull);
    String sql = Db.getSql("department_query");
    List<Department> list = dao.find(sql, id);
    if (null != list && list.size() > 0) {
      result.setResult(Result.kSuccess);
      result.setObject(Models.compressList(list));
    }
    return result;
  }

  public static JSONObject list(int page, int size, JSONArray filter,
      JSONArray search) {
    String select = Db.getSql("department-list-sec");
    String except = Db.getSql("department-list-exp");

    String keyWord;
    String logical;
    Object type = Strings.get(filter, "type", false);
    Object s = Strings.get(search, "keyWord", true);
    if (null != type) {
      int tid = Integer.parseInt(type.toString());
      if (tid > 0) {
        except += " AND dt.id = " + tid;
      }
    }
    if (null != s) {
      JSONObject object = JSONObject.parseObject(s.toString());
      keyWord = object.getString("keyWord");
      logical = object.getString("logical");
      except +=
          "\n" + logical + " d.name LIKE concat('%','" + keyWord + "','%')";
    }
    Page<Record> pages = Db.paginate(page, size, select, except);
    return Models.compressPage(pages);
  }

  public static ModelResult list(JSONArray filter) {
    ModelResult result = new ModelResult(Result.kSuccess);
    String select = Db.getSql("department-list-sec");
    String except = Db.getSql("department-list-exp");
    Object type = Strings.get(filter, "type", false);
    if (null != type) {
      int tid = Integer.parseInt(type.toString());
      if (tid > 0) {
        except += " AND dt.id = " + tid;
      }
    }
    List<Record> list = Db.find(select + except);
    result.setObject(Models.compressLists(list));
    return result;
  }

  public static JSONArray getProcess(int did) {
    JSONArray process = new JSONArray();
    JSONArray approvers = new JSONArray();
    Department department = Department.dao.findById(did);
    if (null != department && null != department.getCharge()) {
      approvers.add(department.getCharge());
      JSONObject object = new JSONObject();
      object.put("approvers", approvers);
      object.put("minimum_pass", 1);
      process.add(object);
    }
    return process;
  }

  public static int getCharge(int did) {
    int charge = Consts.DEFAULT_BAD_ID;
    Department department = Department.dao.findById(did);
    if (null != department && null != department.getCharge()) {
      charge = department.getCharge();
    }
    return charge;
  }
}
